<template>
  <div class="factory_sub_account table-theme">
    <head-title :title="isType == 0 ? '数据--转账打款列表':'审核--工厂转账审核'"></head-title>
    <el-card>
      <div class="top-menu">
        <div class="menu-left">
          <tabs-view :list="classifyList" :tabsIndex="type" @roleType="roleType"></tabs-view>
           <select-view
            :option-list="payTypeOptions"
            :value="form.pay_type"
            placeholder="请选择结算类型"
            type="pay_type"
            @value="setSelectValue"
          >
          </select-view>
          <select-view
            :option-list="paymentTyleOptions"
            :value="form.type"
            placeholder="请选择付款类型"
            type="type"
            @value="setSelectValue"
          >
          </select-view>
        </div>
        <div class="menu-right">
          <refresh-button @refresh="refreshPage"></refresh-button>
          <search-view
            :option="selectList"
            @value="goSearch"
            @clear="refreshPage"
          ></search-view>
        </div>
      </div>
      <p class="data-number">共{{ total }}条数据</p>
      <el-table :data="list" stripe>
        <el-table-column
          label="序号"
          align="center"
          prop="order_id"
        ></el-table-column>
        <el-table-column
          label="工厂名字"
          align="center"
          prop="factory_name"
        ></el-table-column>
        <el-table-column
          label="工厂编号"
          align="center"
          prop="factory_sn"
        ></el-table-column>
        <el-table-column
          label="订单编号"
          align="center"
          prop="order_sn"
        ></el-table-column>
        <el-table-column label="结算类型" align="center">
          <template slot-scope="scope">
            <span>{{
              scope.row.pay_type == 1
                ? "预付款"
                : scope.row.pay_type == 2
                ? "全款"
                : "月结"
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="支付编号" align="center" prop="random_char">

        </el-table-column>
        <el-table-column
          label="付款占比"
          align="center"
          prop="advance_charge"
        ></el-table-column>
        <el-table-column label="付款类型" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.type == 1 ? "预付款" : "尾款" }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="收款账户名"
          align="center"
          prop="bank_account_name"
        ></el-table-column>
        <el-table-column
          label="开户行"
          align="center"
          prop="bank_deposit"
        ></el-table-column>
        <el-table-column
          label="流水号"
          align="center"
          prop="yurref"
        ></el-table-column>
        <el-table-column label="结算金额" align="center" prop="money">
        </el-table-column>
        <el-table-column label="总收款" align="center" prop="total_money"></el-table-column>
        <el-table-column label="支付状态" align="center">
          <template slot-scope="scope">
            <span>{{
              scope.row.pay_status == 0
                ? "未支付"
                : scope.row.pay_status == 1
                ? "支付成功"
                : scope.row.pay_status == 2
                ? "支付失败"
                :""
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <span>{{
              scope.row.status == 1
                ? "待审核"
                : scope.row.status == 2
                ? "提交审核"
                : scope.row.status == 3
                ? "通过"
                : scope.row.status == 4
                ? "拒绝"
                : ""
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="打款时间"
          align="center"
          prop="up_time"
        ></el-table-column>
        <el-table-column
          label="时间"
          align="center"
          prop="add_time"
        ></el-table-column>
        <el-table-column label="操作" align="center" width="180">
          <template slot-scope="scope">
                <el-button
                  class="margin-top6px"
                  type="primary"
                  size="mini"
                  @click="onUpType('apply', scope.row.factory_pay_id)"
                  v-if="type == 1 && admin_id != 3"
                  >发起申请</el-button
                >
                <el-button
                  class="margin-top6px"
                  type="primary"
                  size="mini"
                  @click="onUpType('sync', scope.row.factory_pay_id,scope.row.factory_sn)"
                  v-if="scope.row.status == 2 && admin_id != 3"
                  >同步账号</el-button
                >
                <el-button
                  class="margin-top6px"
                  type="primary"
                  size="mini"
                  @click="onUpType('pass', scope.row.factory_pay_id)"
                  v-if="scope.row.status < 3 && scope.row.status != 1 && admin_id != 3"
                  >通过</el-button
                >
                <el-button
                  class="margin-top6px"
                  type="danger"
                  size="mini"
                  @click="onUpType('refuse', scope.row.factory_pay_id)"
                  v-if="scope.row.status < 3 && scope.row.status != 1 && admin_id != 3"
                  >拒绝</el-button
                >
                <el-button
                  class="margin-top6px"
                  type="primary"
                  size="mini"
                  @click="onOpenPage(scope.row.order_id)"
                  >查看订单</el-button
                >
          </template>
        </el-table-column>
      </el-table>
      <div class="pages">
        <el-pagination
          background
          layout="prev, pager, next, jumper"
          :total="total"
          @current-change="init"
          :current-page="page"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import SelectView from "@/components/SelectView";
import TabsView from '@/components/TabsView'
export default {
  components:{
    SelectView,
		TabsView
  },
  data() {
    return {
      classSelect: "",
      selectContent: "",
      selectList: [
        {
          value: 0,
          label: "订单编号",
        },
        {
          value: 1,
          label: "工厂编号",
        }
      ],
      classifyList: [
          "全部","待审核","提交审核","通过","拒绝"
      ],
      type: 0,
      payTypeOptions: [
        {
          value: "",
          label: "全部",
        },
        {
          value: "1",
          label: "预付款",
        },
        {
          value: "2",
          label: "全款",
        },
        {
          value: "3",
          label: "月结",
        },
      ],
      paymentTyleOptions: [
        {
          value: "",
          label: "全部",
        },
        {
          value: "1",
          label: "预付款",
        },
        {
          value: "2",
          label: "尾款",
        },
      ],
      form: {
        pay_type: "",
        type: "",
        status: "",
      },
      list: [],
      total: 0,
      page: 0,
      is_order:false,
      admin_id:"",
      is_submit:true,
      isType:0
    };
  },
  methods: {
    init(e) {
      this.page = e;
      let data = {
        page: e - 1 ? e - 1 : 0,
        limit: 10,
        pay_type: this.form.pay_type,
        type: this.form.type,
        status: this.form.status === 0 ? '':this.form.status,
      };
      if(this.type == 0){
        data.get_all = 1
      }
      //   0订单编号    1工厂编号  2订单id
      if (this.classSelect == 0) {
        data.order_sn = this.selectContent;
      } else if (this.classSelect == 1) {
        data.factory_sn = this.selectContent;
      }
      if (this.is_order) {
        data.order_id = this.$route.query.order_id;
      }
      if(this.admin_id == 3){
        data.factory_sn = localStorage.getItem('factory_sn')
      }
      this.$http(this.$ApiList.lists, data).then((res) => {
        this.list = res.datas.data;
        this.total = res.datas.count;
      }).then(()=>{
        this.is_order = false;
      });
    },
    // 刷新
    refreshPage() {
      this.classSelect = "";
      this.selectContent = "";
      this.form.pay_type = "";
      this.form.type = "";
      this.form.status = "";
      this.type = 0;
      this.init();
    },
    goSearch(classSelect, selectContent) {
      this.classSelect = classSelect;
      this.selectContent = selectContent;
      this.init();
    },
    roleType(index) {
      this.type = index;
      this.form.status = index;
      if(this.admin_id == 3){
        if(index > 1){
          this.form.status = index+1
        }
      }
      this.classSelect = 0;
      this.selectContent = '';
      this.init();
    },
    onUpType(type, id,sn) {
      if (type === "pass") {
        if(this.is_submit){
          this.is_submit = false;
          this.$http(this.$ApiList.AdminfactorypayUp, {
            status: 3,
            factory_pay_id: id,
          }).then((res) => {
            this.$message.success(res.datas);
            this.init(this.page);
            this.is_submit = true
          });
        }else{
          this.$message.error('正在提交，请勿重复操作！')
        }
      } else if (type === "refuse") {
        this.$confirm("拒绝后不可恢复,是否确认拒绝?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.$http(this.$ApiList.AdminfactorypayUp, {
              status: 4,
              factory_pay_id: id,
            }).then((res) => {
              this.$message.success(res.datas);
              this.init(this.page);
              // loading.close();
            });
          })
          .catch(() => {});
      }else if(type === 'sync'){
        let data = {
          factory_pay_id:id,
          factory_sn:sn
        }
        this.$http(this.$ApiList.Sync,data).then(res=>{
          this.$message.success(res.datas);
          this.init(this.page);
          // loading.close();
        })
      }else if(type === 'apply'){
        this.$http(this.$ApiList.AdminfactorypayUp,{
          status:2,
          factory_pay_id:id
        }).then(res=>{
          this.$message.success(res.datas);
          this.init(this.page);
          // loading.close();
        })
      }
    },
    onOpenPage(order_id) {
      this.$router.push({ path: "/order_details", query: { id: order_id } });
    },
    setSelectValue(obj){
      const { type, value } = obj
      switch (type) {
        case 'pay_type':
          this.form.pay_type = value
          break;
        case 'type':
          this.form.type = value
          break;
        default:
          break;
      }
      this.init()
    }
  },
  created() {
    if(this.$route.query.order_id){
      this.is_order = true;
    }
    if(this.$route.query.type){
      this.isType = this.$route.query.type
    }
    this.admin_id = localStorage.getItem('admin_id');
    if(this.admin_id == 3){
      this.selectList = [
        {
          value:0,
          label:"订单编号"
        }
      ]
      this.classifyList = ["全部","待审核","通过","拒绝"]
    }
    this.init();
  },
};
</script>

<style lang="scss" scoped>
.factory_sub_account {
  .menu-left {
    display: flex;
    align-items: center;
    }
  .margin-top6px{
    margin-top: 6px;
  }
}
</style>